//
//  WelcomeViewController.swift
//  SinaWeiBo20
//
//  Created by tarena on 16/3/23.
//  Copyright © 2016年 tarena. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


class WelcomeViewController: UIViewController {
    
    //懒加载 所有的视图控件
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconImage: UIImageView = UIImageView()
    
    private lazy var welcomeLabel: UILabel = {
        
        let l = UILabel()
        l.textColor = UIColor.blackColor()
        l.font = UIFont.systemFontOfSize(19)
        l.textAlignment = .Center
        l.text = (UserAccountViewModel().userName ?? "") + "欢迎归来"
        l.alpha = 0
        l.sizeToFit()
        return l
    }()
    
    override func loadView() {
        view = backImage
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
        
        
        //外部改变约束
        self.iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        /*
        一般在自动布局中使用动画效果  不建议直接修改非让阿门
        
        自动布局系统 会根据设置的约束 在layoutSubViews方法中自动修改frame
        frame 是有自动布局系统来控制的
        如果希望能够提前刷新修改的约束效果  需要调用 layoutIfNeeded() 让约束提前刷新 ,更新frame
        */
        
        //动画效果 由近及远 涉及到加速度
        //弹簧系数  usingSpringWithDamping 0~1
        //initialSpringVelocity 加速度
        //弹簧效果 和加速度之间的关系 ： 弹簧效果 * 10 = 加速度
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 8, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_ ) -> Void in
                UIView.animateWithDuration(1.5, animations: { () -> Void in
                        self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                    //发出通知
                    NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
                })
                
        }
     
    }
    
    //添加子视图
    private func setupUI() {
        //1.添加子视图
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        //2.设置布局
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //加载图片
        iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        
        //给图片处理成圆角
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        
    }
    

}
